/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.AgentExterieur;
import fr.ipst.spot.entites.Organisme;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public class FormAgentExterieur extends FormAbstractHtmlEntite<AgentExterieur> {

    private String nom;
    private String prenom;
    private String telephone;
    private String mail;
    private String fonction;
    private FormOrganismeId idOrganisme;

    /**
     *
     * @param entity
     */
    public FormAgentExterieur(AgentExterieur entity) {
        super(new FormAgentExterieurId(entity.getId()), entity);
        this.nom = entity.getNom();
        this.prenom = entity.getPrenom();
        this.telephone = entity.getTelephone();
        this.mail = entity.getMail();
        this.fonction = entity.getFonction();
        this.idOrganisme = new FormOrganismeId(entity.getOrganisme().getIdOrganisme());
    }

    /**
     *
     * @param request
     */
    public FormAgentExterieur(HttpServletRequest request) {
        super(new FormAgentExterieurId(request), request);
        this.nom = this.getNonNullStringFromRequest(request, "nom");
        this.prenom = this.getNonNullStringFromRequest(request, "prenom");
        this.mail = this.getNonNullStringFromRequest(request, "mail");
        this.telephone = this.getNonNullStringFromRequest(request, "telephone");
        this.fonction = this.getNonNullStringFromRequest(request, "fonction");
        this.idOrganisme = new FormOrganismeId(request);
    }

    @Override
    protected void validate(boolean creation) throws SpotExceptionForm {
        boolean erreur = false;
        if (!creation) {
            try {
                this.formId.validate();
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(new SpotExceptionForm("Agent Exterieur fournit invalide"));
            }
        }
        try {
            this.isDefaultLengthOneLine(nom, "nom");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isDefaultLengthOneLine(prenom, "prenom");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isDefaultLengthOneLine(fonction, "fonction");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isEmailAdresse(mail);
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isPhoneNumber(telephone, "téléphonne");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.idOrganisme.validate();
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(new SpotExceptionForm("Orgnaisme fournit invalide"));
        }
        if (erreur) {
            throw new SpotExceptionForm(msgErreurValidate);
        }
    }

    @Override
    public AgentExterieur getEntityToPersist() throws SpotExceptionForm {
        return this.garnir(new AgentExterieur(), true);
    }

    @Override
    protected AgentExterieur garnir(AgentExterieur entityToFeed, boolean creation) throws SpotExceptionForm {
        entityToFeed.setFonction(fonction);
        entityToFeed.setMail(mail);
        entityToFeed.setNom(nom);
        entityToFeed.setOrganisme(new Organisme(idOrganisme.getId()));
        entityToFeed.setPrenom(prenom);
        entityToFeed.setTelephone(telephone);
        return entityToFeed;
    }

    /**
     *
     * @return
     */
    public String getFonction() {
        return fonction;
    }

    /**
     *
     * @param fonction
     */
    public void setFonction(String fonction) {
        this.fonction = fonction;
    }

    /**
     *
     * @return
     */
    public Integer getIdOrganisme() {
        return idOrganisme.getId();
    }

    /**
     *
     * @param idOrganisme
     */
    public void setIdOrganisme(Integer idOrganisme) {
        this.idOrganisme.setId(idOrganisme);
    }

    /**
     *
     * @return
     */
    public String getMail() {
        return mail;
    }

    /**
     *
     * @param mail
     */
    public void setMail(String mail) {
        this.mail = mail;
    }

    /**
     *
     * @return
     */
    public String getNom() {
        return nom;
    }

    /**
     *
     * @param nom
     */
    public void setNom(String nom) {
        this.nom = nom;
    }

    /**
     *
     * @return
     */
    public String getPrenom() {
        return prenom;
    }

    /**
     *
     * @param prenom
     */
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    /**
     *
     * @return
     */
    public String getTelephone() {
        return telephone;
    }

    /**
     *
     * @param telephone
     */
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
}
